import pytest
from functools import wraps

# 计分装饰器
def score(points):
    def decorator(f):
        f.score = points
        @wraps(f)
        def wrapper(*args, **kwargs):
            return f(*args, **kwargs)
        return wrapper
    return decorator

# 添加pytest钩子确保元数据被收集
@pytest.hookimpl(tryfirst=True)
def pytest_runtest_protocol(item, nextitem):
    if hasattr(item.function, 'score'):
        item.user_properties.append(("score", item.function.score))

def pytest_json_modifyreport(json_report):
    for test in json_report.get("tests", []):
        for prop in test.get("user_properties", []):
            if "score" in prop:
                test.setdefault("metadata", {})["score"] = prop["score"]